/***
 * Copyright (c) 2010 readyState Software Ltd
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License. You may obtain
 * a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */

package net.kts.attraction.search.overlay;

import java.util.ArrayList;

import net.kts.attraction.search.model.Address;
import net.kts.attraction.subway.SubwayListActivity;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;


public class StartDestMyItemizedOverlay extends StartDestItemizedOverlay<OverlayItem> {

	private ArrayList<OverlayItem> m_overlays = new ArrayList<OverlayItem>();
	private Context c;
	
	public StartDestMyItemizedOverlay(Drawable defaultMarker, MapView mapView) {
		super(boundCenter(defaultMarker), mapView);
		c = mapView.getContext();
	}

	public void addOverlay(OverlayItem overlay) {
	    m_overlays.add(overlay);
	    populate();
	}

	@Override
	protected OverlayItem createItem(int i) {
		return m_overlays.get(i);
	}

	@Override
	public int size() {
		return m_overlays.size();
	}

	@Override
	protected boolean onBalloonTap(int index) {
//		Toast.makeText(c, "onBalloonTap for overlay index " + index,
//				Toast.LENGTH_LONG).show();
		
		AlertDialog.Builder builder2 = new AlertDialog.Builder(c);			
		builder2.setMessage("Would you like Subway Guide?");
		
		final GeoPoint geoPoint = m_overlays.get(index).getPoint();
		
		builder2.setPositiveButton("YES", new OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				Address address = new Address();
				address.setLatitude(String.valueOf(geoPoint.getLatitudeE6() * 0.000001));
				address.setLongitude(String.valueOf(geoPoint.getLongitudeE6() * 0.000001));
				
				Intent intent = new Intent(c, SubwayListActivity.class);
				Bundle bundle = new Bundle();
				bundle.putSerializable("address", address);
				intent.putExtras(bundle);				
				
				c.startActivity(intent);
			}
		});
		builder2.setNegativeButton("NO", null);
		builder2.show();
		
		
		
		
		return true;
	}
	
}
